package com.tsd.jxc.req;

import com.alibaba.fastjson.JSON;
import com.tsd.core.datasource.SpringContextUtil;
import com.tsd.core.utils.DoubleUtil;
import com.tsd.core.utils.HlpUtils;
import com.tsd.core.utils.ListUtil;
import com.tsd.jxc.buy.entity.JxcBuyReturnBill;
import com.tsd.jxc.buy.entity.JxcBuyReturnBillDetailExt;
import com.tsd.jxc.buy.entity.JxcBuyReturnBillExt;
import com.tsd.jxc.goods.dao.JxcGoodsMapper;
import com.tsd.jxc.goods.vo.JxcGoodsVo;
import com.tsd.jxc.setting.JxcSettingUtil;
import com.tsd.jxc.utils.ClothingUtil;
import com.tsd.jxc.vo.JxcSettingVo;
import com.tsd.utils.AutoInjectUtils;

import java.math.BigDecimal;
import java.util.*;

/**
 * @ClassName: JxcBuyReturnBillReqParamsVo
 * @Description: TODO
 * @Author: Hillpool
 * @Date: 2023/3/15 10:17
 * @Version: 1.0
 */
public class JxcBuyReturnBillReqParamsVo extends JxcBuyReturnBill {

    private List<JxcBuyReturnBillDetailReqParamsVo> detail_list;

    public List<JxcBuyReturnBillDetailReqParamsVo> getDetail_list() {
        return detail_list;
    }

    public void setDetail_list(List<JxcBuyReturnBillDetailReqParamsVo> detail_list) {
        this.detail_list = detail_list;
    }

    public JxcBuyReturnBillExt convert() {
        JxcBuyReturnBillExt billExt = JSON.parseObject(JSON.toJSONString(this), JxcBuyReturnBillExt.class);
        List<JxcBuyReturnBillDetailExt> detailExtList = billExt.getDetail_list();
        if (JxcSettingUtil.isClothing()) {
            detailExtList = new ArrayList<>();
            int sn = 1;
            for (JxcBuyReturnBillDetailReqParamsVo paramsVo : this.getDetail_list()) {
                for (Map.Entry<String, BigDecimal> item : paramsVo.getPreset_qty_map().entrySet()) {
                    if (HlpUtils.isEmpty(item.getValue()) || item.getValue().doubleValue() == 0) {
                        continue;
                    }
                    JxcBuyReturnBillDetailExt detailExt = JSON.parseObject(JSON.toJSONString(paramsVo), JxcBuyReturnBillDetailExt.class);
                    Map<String, Long> idMap = paramsVo.getPreset_size_id_map();
                    if (idMap != null && idMap.containsKey(item.getKey())) {
                        detailExt.setId(idMap.get(item.getKey()));
                    }
                    Map<String, String> buyMap = paramsVo.getPreset_buy_map();
                    if (buyMap != null && buyMap.containsKey(item.getKey())) {
                        String[] strings = buyMap.get(item.getKey()).split("_");
                        if (strings.length == 2) {
                            detailExt.setBuy_order_sid(strings[0]);
                            detailExt.setBuy_detail_sid(strings[1]);
                        }
                    }
                    Map<String, String> goodsMap = paramsVo.getPreset_goods_map();
                    if (goodsMap != null && goodsMap.containsKey(item.getKey())) {
                        detailExt.setGoods_sid(goodsMap.get(item.getKey()));
                    }
                    Map<String, String> productMap = paramsVo.getPreset_product_map();
                    if (productMap != null && productMap.containsKey(item.getKey())) {
//                        detailExt.setProduct_sid(productMap.get(item.getKey()));
                    }
                    detailExt.setQty(item.getValue());
                    detailExt.setSn(sn);
                    detailExtList.add(detailExt);
                    sn += 1;
                }
            }
        }
        billExt.setDetail_list(detailExtList);
        return billExt;
    }

    public static JxcBuyReturnBillReqParamsVo transfer(JxcBuyReturnBillExt billExt, JxcSettingVo settingVo) {
        if (billExt == null) {
            return null;
        }
        JxcBuyReturnBillReqParamsVo paramsVo = JSON.parseObject(JSON.toJSONString(billExt), JxcBuyReturnBillReqParamsVo.class);
        List<JxcBuyReturnBillDetailExt> detailExtList = billExt.getDetail_list();
        List<JxcBuyReturnBillDetailReqParamsVo> voList = new ArrayList<>();
        if (!HlpUtils.isEmptyList(detailExtList)) {
            List<String> sizeList = ClothingUtil.getClothing_preset_size_list(settingVo);
            JxcGoodsMapper jxcGoodsMapper = SpringContextUtil.getBean(JxcGoodsMapper.class);
            Map<String, Object> params = new HashMap<>();
            params.put("goods_list", ListUtil.map(detailExtList, JxcBuyReturnBillDetailExt::getGoods_sid));
            List<JxcGoodsVo> goodsVoList = jxcGoodsMapper.select4ClothingByPage(params, null);
            for (JxcGoodsVo goodsVo : goodsVoList) {
                List<JxcBuyReturnBillDetailExt> filterDetailList = null;//ListUtil.filter(detailExtList, t -> goodsVo.containsProduct(t.getProduct_sid()));
                if (HlpUtils.isEmptyList(filterDetailList)) {
                    continue;
                }
                JxcBuyReturnBillDetailReqParamsVo detailReqParamsVo = JSON.parseObject(JSON.toJSONString(filterDetailList.get(0)), JxcBuyReturnBillDetailReqParamsVo.class);
                AutoInjectUtils.clearBaseData(detailReqParamsVo);
                detailReqParamsVo.setSn(filterDetailList.get(0).getSn());
//                detailReqParamsVo.setPreset_product_map(goodsVo.getClothing_preset_product_map());
                Map<String, String> presetProductMap = null;//goodsVo.getClothing_preset_product_map();
                Map<String, BigDecimal> presetQtyMap = new HashMap<>();
                Map<String, Long> presetSizeIdMap = new HashMap<>();
                Map<String, String> presetGoodsMap = new HashMap<>();
                Map<String, String> presetBuyMap = new HashMap<>();
                int index = 0;
                for (String size : sizeList) {
                    String productSid = presetProductMap.get(size);
                    if (HlpUtils.isEmpty(productSid)) {
                        index += 1;
                        continue;
                    }
                    JxcBuyReturnBillDetailExt detailExt = null;//ListUtil.get(filterDetailList, t -> productSid.equals(t.getProduct_sid()));
                    if (detailExt == null) {
                        index += 1;
                        continue;
                    }
                    presetQtyMap.put("preset_qty_" + index, detailExt.getQty());
                    presetSizeIdMap.put(size, detailExt.getId());
                    presetGoodsMap.put(size, detailExt.getGoods_sid());
                    presetBuyMap.put(size, String.format("%s_%s", detailExt.getBuy_order_sid(), detailExt.getBuy_detail_sid()));
                    index += 1;
                }
                detailReqParamsVo.setPreset_qty_map(presetQtyMap);
                detailReqParamsVo.setPreset_goods_map(presetGoodsMap);
                detailReqParamsVo.setPreset_buy_map(presetBuyMap);
                detailReqParamsVo.setPreset_size_id_map(presetSizeIdMap);
                double totalQty = 0D, totalAmount = 0D;
                for (JxcBuyReturnBillDetailExt detailExt : filterDetailList) {
                    totalQty = DoubleUtil.add(totalQty, detailExt.getQty());
                    totalAmount = DoubleUtil.add(totalAmount, detailExt.getAmount());
                }
                detailReqParamsVo.setQty(BigDecimal.valueOf(totalQty));
                detailReqParamsVo.setAmount(BigDecimal.valueOf(totalAmount));
                voList.add(detailReqParamsVo);
            }
        }
        voList = ListUtil.sorted(voList, Comparator.comparing(JxcBuyReturnBillDetailReqParamsVo::getSn));
        paramsVo.setDetail_list(voList);
        return paramsVo;
    }
}
